package cms.simmytech.service;

import cms.simmytech.mapper.TbMomentCommentMapper;
import cms.simmytech.mapper.TbMomentMapper;
import cms.simmytech.mapper.TbMomentMapper;
import cms.simmytech.model.*;
import cms.simmytech.model.TbMoment;
import cms.simmytech.response.MomentDetailResponse;
import cms.simmytech.util.DateUtil;
import cms.simmytech.util.ResultCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class MomentService {
    @Autowired
    private TbMomentMapper mapper;
    @Autowired
    private TbMomentCommentMapper momentCommentMapper;
    public PageInfo<TbMoment> getPageList(Integer pageNum, Integer pageSize, String name, Byte momentType, Byte momentState) {
        TbMomentExample example = new TbMomentExample();
        TbMomentExample.Criteria criteria = example.createCriteria();
        //对标题名称进行处理--便于模糊查询
        if(!"".equals(name)&&null!=name){
            criteria.andContentLike("%"+name+"%")	;
        }
        if (momentType!=-99){
            criteria.andMomentTypeEqualTo(momentType);
        }
        if (momentState!=-99){
            criteria.andMomentStateEqualTo(momentState);
        }
        //根据contentId进行降序
        example.setOrderByClause("moment_id  desc");
        PageHelper.startPage(pageNum, pageSize);
        List<TbMoment> list = mapper.selectByExampleWithBLOBs(example);
        PageInfo<TbMoment> pageInfo = new PageInfo<TbMoment>(list);
        return pageInfo;
    }

    public TbMoment getBean(Integer id) {
        return  mapper.selectByPrimaryKey(id);
    }

    public void save(TbMoment bean) {
        //对Id进行判断：id为null证明是添加，否则为更新
        if (bean.getMomentId()== null) {
            Integer now = DateUtil.getIntStampTime();
            bean.setCreateTime(now);
            mapper.insertSelective(bean);

        } else {
            mapper.updateByPrimaryKeySelective(bean);
        }
    }

    public void delete(int id) {
        mapper.deleteByPrimaryKey(id);
    }
    //新增评论删除
    public void del(int id) {
        TbMomentComment mc = momentCommentMapper.selectByPrimaryKey(id);

        TbMoment moment = mapper.selectByPrimaryKey(mc.getMomentId());
        if(moment.getCommentCount()>0){
            moment.setCommentCount(moment.getCommentCount()-1);
            mapper.updateByPrimaryKey(moment);
        }

        momentCommentMapper.deleteByPrimaryKey( id );
    }

    public PageInfo<TbMoment> getContentList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        TbMomentExample example=new TbMomentExample();
        example.setOrderByClause(" content_id  desc");
        List<TbMoment> list= mapper.selectByExample(example);
        PageInfo<TbMoment> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }



    public void deleteContent(int id) {
        mapper.deleteByPrimaryKey(id);
    }
    public void AddContent(TbMoment TbMoment){
        mapper.insert(TbMoment);
    }

    public TbMoment getContentByContentID(int id){
        return mapper.selectByPrimaryKey(id);
    }
    public void updateContentByContentId(TbMoment TbMoment){
        mapper.updateByPrimaryKey(TbMoment);
    }

    public MomentDetailResponse getDetailById(Integer id) {
        MomentDetailResponse response = new MomentDetailResponse(ResultCode.OPERATION_ERROR);
        TbMoment bean = mapper.selectByPrimaryKey(id);
        response.getData().setMoment(bean);
        TbMomentCommentExample example=new TbMomentCommentExample();
        example.createCriteria().andMomentIdEqualTo(bean.getMomentId());
        List<TbMomentComment> momentCommentList = momentCommentMapper.selectByExampleWithBLOBs(example);
        response.getData().setCommentList(momentCommentList);
        response.setResponse(ResultCode.SUCCESS);
        return  response;
    }
}
